//
//  RegisterView.m
//  LClass
//
//  Created by 严军 on 16/5/30.
//  Copyright © 2016年 YanJun. All rights reserved.
//

#import "RegisterView.h"

@implementation RegisterView
{
    BOOL checked;//是否选框
    UIButton* btncheck;//选框
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)init
{
    self=[super init];
    
    if(self)
    {
        [self initLayout];
    }
    return self;
}

-(void)initLayout
{
    self.backgroundColor=ThemeBackgroundColor;
    
    //设置用户名输入框
    _TF_userName=[[TextFieldValidator alloc]init];
    [self addSubview:_TF_userName];
    [_TF_userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(50);
        make.centerX.mas_equalTo(self.center.x);
        make.width.equalTo(self.mas_width).multipliedBy(0.7);
        make.height.mas_equalTo(40);
    }];
    [[Utility sharedInstance] setLayerView:_TF_userName borderW:2 borderColor:ThemeGrayColor radius:3];
    _TF_userName.delegate=self;
    _TF_userName.placeholder=@"请输入用户名";
    _TF_userName.presentInView=self;
    _TF_userName.validateOnCharacterChanged=NO;
    _TF_userName.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
    _TF_userName.validateOnResign=NO;
    _TF_userName.secureTextEntry=NO;
    [_TF_userName addRegx:Regex_userName withMsg:@"请输入合法用户名"];
    
    //设置密码输入框
    _TF_password=[[TextFieldValidator alloc]init];
    [self addSubview:_TF_password];
    [_TF_password mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_TF_userName.mas_bottom).offset(10);
        make.centerX.mas_equalTo(self.center.x);
        make.width.equalTo(self.mas_width).multipliedBy(0.7);
        make.height.mas_equalTo(40);
    }];
    [[Utility sharedInstance] setLayerView:_TF_password borderW:2 borderColor:ThemeGrayColor radius:3];
    _TF_password.delegate=self;
    _TF_password.placeholder=@"请输入密码";
    _TF_password.presentInView=self;
    _TF_password.validateOnCharacterChanged=NO;
    _TF_password.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
    _TF_password.validateOnResign=NO;
    _TF_password.secureTextEntry=YES;
    [_TF_password addRegx:Regex_password withMsg:@"密码长度应为6-12位"];
    
    //设置电话输入框
    _TF_phone=[[TextFieldValidator alloc]init];
    [self addSubview:_TF_phone];
    [_TF_phone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_TF_password.mas_bottom).offset(10);
        make.centerX.mas_equalTo(self.center.x);
        make.width.equalTo(self.mas_width).multipliedBy(0.7);
        make.height.mas_equalTo(40);
    }];
    [[Utility sharedInstance] setLayerView:_TF_phone borderW:2 borderColor:ThemeGrayColor radius:3];
    _TF_phone.delegate=self;
    _TF_phone.placeholder=@"请输入手机号";
    _TF_phone.presentInView=self;
    _TF_phone.validateOnCharacterChanged=NO;
    _TF_phone.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
    _TF_phone.validateOnResign=NO;
    _TF_phone.secureTextEntry=YES;
    [_TF_phone addRegx:Regex_phone withMsg:@"请输入合法手机号"];
    
    //设置按钮注册
    _Btn_regist=[[UIButton alloc]init];
    [self addSubview:_Btn_regist];
    [_Btn_regist mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_TF_phone.mas_bottom).offset(10);
        make.centerX.equalTo(self);
        make.width.equalTo(self.mas_width).multipliedBy(0.8);
        make.height.mas_equalTo(40);
    }];
    [[Utility sharedInstance] setLayerView:_Btn_regist borderW:1 borderColor:ThemeGrayColor radius:3];
    _Btn_regist.backgroundColor=[UIColor redColor];
    _Btn_regist.titleLabel.textColor=[UIColor whiteColor];
    _Btn_regist.titleLabel.font=[UIFont boldSystemFontOfSize:16];
    [_Btn_regist setTitle:@"注 册" forState:UIControlStateNormal];
    [_Btn_regist addTarget:self action:@selector(clickRegister:) forControlEvents:UIControlEventTouchUpInside];
    
    //设置按钮选框
    btncheck=[[UIButton alloc]init];
    [self addSubview:btncheck];
    [btncheck mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_Btn_regist.mas_bottom).offset(15);
        make.left.equalTo(_Btn_regist);
        make.width.height.mas_equalTo(20);
    }];
    [[Utility sharedInstance] setLayerView:btncheck borderW:1 borderColor:ThemeGrayColor radius:3];
    [btncheck setImage:[UIImage imageNamed:@"checkbox_unselected"] forState:0];
    checked=NO;
    [btncheck addTarget:self action:@selector(clickChecked) forControlEvents:UIControlEventTouchUpInside];
    
    //设置文字
    UILabel* labTip=[[UILabel alloc]init];
    [self addSubview:labTip];
    [labTip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(btncheck.mas_centerY);
        make.left.equalTo(btncheck.mas_right).offset(8);
    }];
    labTip.font=[UIFont boldSystemFontOfSize:12];
    labTip.textColor=[UIColor grayColor];
    labTip.text=@"我已阅读并同意";
    
    //设置按钮《服务条款》
    UIButton* btnSeverce=[[UIButton alloc]init];
    [self addSubview:btnSeverce];
    [btnSeverce mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labTip);
        make.left.equalTo(labTip.mas_right);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(16);
    }];
    [btnSeverce setTitleColor:[UIColor redColor] forState:0];
    [btnSeverce setTitle:@"《服务条款》" forState:0];
     btnSeverce.titleLabel.font=[UIFont systemFontOfSize:12];
}

-(void)clickChecked
{
    if (checked==NO) {
        [btncheck setImage:[UIImage imageNamed:@"checkbox_selected"] forState:0];
        checked=YES;
    }else
    {
    [btncheck setImage:[UIImage imageNamed:@"checkbox_unselected"] forState:0];
        checked=NO;
    }
}

-(void)clickRegister:(id)sender
{
    BOOL ifName=[_TF_userName validate];
    BOOL ifPass=[_TF_password validate];
    BOOL ifPhone=[_TF_phone validate];
    
    if (checked&&ifName&&ifPass&&ifPhone) {
        NSDictionary* dic=@{@"username":_TF_userName.text,@"password":_TF_password.text,@"phone":_TF_phone.text};
        _registerblock(dic);
    }
}


@end
